<?php
if (!defined('IN_WONDERSHARE'))
{
die('Hacking attempt');
}
require('./classes/module/module_tag.php');
class TagControl
{
	var $tag_m;
	function __construct()
	{
	
		$this->tag_m = new TagModule();
	}
	/**
	* 根据id得到文章信息
	*
	* @param   int         $tag_id     截取的长度
	*
	* @return  array
	*/
	function  get_tag_id_info($tag_id)
	{
		$sql = "SELECT tag_id,tag_name,is_hot,click_count,tag_type,tag_web FROM tag WHERE tag_id = $tag_id";
		return $this->tag_m->get_tag_info($sql);
	}
	/**
	* 增加或者修改tag
	*
	* @param   string      $type            操作类型
	* @param   int         $tag_id          标签id
	* @param   string      $tag_name     	     标签名称
	* @param   int         $click_count     点击数量
	* @param   int         $is_hot     	     是否热门
	* @param   int         $tag_type        标签类型 （话题类型 还是产品类型）
	* @param   int         $tag_web    	     标签属于那个网站
	* @return  int
	*/
	function  operate_tag($type,$tag_id,$tag_name,$click_count,$is_hot,$tag_type,$tag_web)
	{
		if($type == 'add')
		{
			$sql ="INSERT INTO tag (tag_name,click_count,is_hot,tag_type,tag_web,add_time,add_user,update_user,update_time) 
			VALUES ('$tag_name',$click_count,$is_hot,$tag_type,$tag_web,'".$_SERVER['REQUEST_TIME']."','".$_SESSION['AName']."',
			'".$_SESSION['AName']."','".$_SERVER['REQUEST_TIME']."') ";
		}elseif($type == 'edit')
		{
			$sql = "UPDATE tag SET tag_name = '$tag_name',click_count = $click_count,update_user = '".$_SESSION['AName']."'
			,is_hot = $is_hot,tag_type = $tag_type ,tag_web =$tag_web,update_time = '".$_SERVER['REQUEST_TIME']."',modify_status 
			= 1 WHERE tag_id = $tag_id ";
		}
		return $this->tag_m->change_info($sql);	 	
	}
	/**
	* 批量删除标签
	*
	* @param   string         $list_id    标签字符串
	*
	* @return  int
	*/
	function delete_tag($list_id)
	{
		$sql = "UPDATE  tag SET  is_delete = 1 WHERE tag_id IN ($list_id) ";
		
		return $this->tag_m->change_info($sql);
	}
	/**
	* ajax修改标签
	*
	* @param   int        $tag_id          标签id
	* @param   string     $change_value    要修改的值
	* @param   string     $field           数据字段
	* @param   int        $type            操作的类型
	* @param   int         $w_type         网站类型
	* @return  int
	*/
	function  change_info($tag_id,$change_value,$field,$type,$w_type = 0)
	{
	
		if($type == 'update_tag')
		{
			$sql = "UPDATE tag SET $field ='$change_value',update_time = '".$_SERVER['REQUEST_TIME']."',
			update_user = '".$_SESSION['AName']."' ,modify_status  = 1 WHERE tag_id  = $tag_id ";
		}
		elseif($type == 'is_repeat_tag')
		{
			$where_str = empty($tag_id)?'':' AND tag_id != '.(int)$tag_id;
			$sql = "SELECT count(*) AS  nums FROM tag   WHERE $field = '$change_value' AND tag_web = $w_type 
			AND is_delete = 0 $where_str ";
			return $GLOBALS['db']->getOne($sql);
		}
		return $this->tag_m->change_info($sql);
	}
	/**
	* 得到数据提交条件
	*
	* @param   int        $is_hot      标签id
	* @param   string     $tag_type    标签类型（产品还是话题）
	* @param   string     $tag_web     标签属于那个网站
	* @param   string     $tag_name    标签名称
	* @return  string
	*/
	function handle_post($is_hot,$tag_type,$tag_web,$tag_name)
	{ 
		$where_str = '';
		if(!empty($is_hot))
		{
			$is_hot = (int)$is_hot == 2?0:(int)$is_hot ;
			$where_str.=' AND is_hot = '.$is_hot;
		}
		if(!empty($tag_type))
		{
			$tag_type = (int)$tag_type == 2?0:(int)$tag_type ;
			$where_str.=' AND tag_type = '.$tag_type;
		}
		if(!empty($tag_web))
		{
		
			$where_str.=' AND tag_web = '.(int)$tag_web;
		}
		if(!empty($tag_name))
		{
			$tag_name = (trim($tag_name));
			$where_str.=" AND tag_name LIKE '%".$tag_name."%' ";
		}
		return  $where_str;
	}
	/**
	* 得到数据提交条件返回url参数
	*
	* @param   int        $is_hot      标签id
	* @param   string     $tag_type    标签类型（产品还是话题）
	* @param   string     $tag_web     标签属于那个网站
	* @param   string     $tag_name    标签名称
	* @return  string
	*/
	function get_url_par($is_hot,$tag_type,$tag_web,$tag_name)
	{ 
		$url_str = '&url_page=1';
		if(!empty($is_hot))
		{
		
			$url_str.='&is_hot='.(int)$is_hot;
		}
		if(!empty($tag_type))
		{
			$url_str.='&tag_type='.(int)$tag_type;
		}
		if(!empty($tag_web))
		{
			$url_str.='&tag_web='.(int)$tag_web;
		}
		if(!empty($tag_name))
		{
			$tag_name = (trim($tag_name));
			$url_str.='&tag_name='.urlencode(str_replace('\\','',$tag_name));
		
		}
		return  $url_str;
	}
	/**
	* 得到分页相关信息
	* @param   int      $w_type         1为wondershare网站，2为aimersoft网站，3为anypdftools.com网站，
	* 4为applemacvideo.com网站，5为www.wondershare.jp网站，6为www.wondershare.de网站
	* @param   int        $page       页码
	* @param   int        $pagesize   每页显示多少条
	* @param   int        $type       为1代表没有条件查询 为2代表有条件查询
	* @param   int        $is_hot     是否热门
	* @param   int        $tag_type   标签类型（产品还是话题）
	* @param   string     $tag_name   标签名称
	* @return  array
	*/
	function get_url_info($w_type,$page,$pagesize = 10,$type,$is_hot = 0,$tag_type =  0,$tag_name = '')
	{
		$url_str = '';
		if($type == 1)
		{
			$sql_p = 'SELECT COUNT(*) AS total FROM tag WHERE is_delete = 0 AND tag_web = '.$w_type;
		}else
		{
			$where_str = $this->handle_post($is_hot,$tag_type,$w_type,$tag_name);
			$url_str = $this->get_url_par($is_hot,$tag_type,$w_type,$tag_name);
			$sql_p = "SELECT COUNT(*) AS total FROM tag WHERE is_delete = 0  $where_str  ";
		}
		$page_info = get_page_info($sql_p,$pagesize,$page);
		$page_url = get_page_url($page,$page_info['page_num'],$url_str);
		return  array('page_info'=>$page_info,'page_url'=>$page_url);
	}
	/**
	* 得到列表或者其它的tag信息
	* @param   int      $w_type         1为wondershare网站，2为aimersoft网站，3为anypdftools.com网站，
	* 4为applemacvideo.com网站，5为www.wondershare.jp网站，6为www.wondershare.de网站
	* @param   string     $type       类型（list为列表 不带搜索 而 list_s 在列表 带有搜索）
	* @param   int        $start      从那行开始
	* @param   int        $pagesize   取多少条记录
	* @param   int        $is_hot     是否热门
	* @param   int        $tag_type   标签类型（产品还是话题）
	* @param   string     $tag_web    标签属于那个网站
	* @param   string     $tag_name   标签名称
	* @return  array
	*/
	function  get_tag_info($w_type,$type,$start = 0,$pagesize = 10,$is_hot = 0,$tag_type =  0,$tag_name = '')
	{
		if($type == 'list')
		{
			$sql = "SELECT tag.pub_time,tag.pub_status,tag.template_url,tag.tag_id,tag.tag_name,tag.is_hot,
			tag.click_count,tag.tag_type,tag_web  ,add_user,update_user ,(SELECT count( * ) FROM 
			topic_article  WHERE is_delete = 0 AND FIND_IN_SET(tag.tag_id,ar_tag_id) AND web_type = $w_type ) AS count
			FROM tag  WHERE is_delete = 0 AND tag_web = $w_type  ORDER BY click_count DESC LIMIT $start,$pagesize ";
			
		}elseif($type == 'list_s')
		{
		
			$where_str = $this->handle_post($is_hot,$tag_type,$w_type,$tag_name);
			$sql = "SELECT tag.pub_time,tag.pub_status,tag.template_url,tag_id,tag_name,is_hot,click_count,
			tag_type,tag_web  ,add_user,update_user ,(SELECT count( * ) FROM topic_article  WHERE is_delete 
			= 0 AND FIND_IN_SET(tag.tag_id,ar_tag_id) AND web_type = $w_type ) AS count FROM tag  WHERE is_delete = 0
			$where_str   ORDER BY click_count DESC LIMIT $start,$pagesize ";
			
		}
		
		return $this->tag_m->get_tag_info($sql,'list');
	}

}
?>